﻿using Castle.DynamicProxy;
using CNKI.TPI.Web.Admin.IBLL;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Interface.Filters;
using CNKI.TPI.Web.Interface.Models;
using CNKI.TPI.Web.Search.IBLL;
using CNKI.TPI.Web.Search.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace CNKI.TPI.Web.Interface.Areas.Resource.Controllers
{
    public class GetDocumentController : BaseApiController
    {
        private ISearchService searchService = null;
        public GetDocumentController(ISearchService searchService)
        {
            this.searchService = generator.CreateInterfaceProxyWithTarget<ISearchService>(searchService, new IInterceptor[] { new LogInterceptor() });
        }

        // GET api/search/5
        /// <summary>
        /// 单库查询
        /// </summary>
        /// <param name="dbID">数据库id</param>
        /// <param name="dbCode">数据库编码</param>
        /// <param name="searchQuery">查询条件</param>
        /// <param name="fieldList">返回字段列表</param>
        /// <param name="catType">导航分类</param>
        /// <param name="catID">分类编码（逗号分隔）</param>
        /// <param name="order">排序</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <returns></returns>
        public object Get(int dbid, string dbcode, string searchquery, string fieldlist, string cattype, string catid, string order, int pageindex, int pagesize)
        {
            OutputEntity result = new OutputEntity();
            try
            {
                IList<SearchQuery> searchQueryList = null;
                if (!string.IsNullOrEmpty(searchquery))
                {
                    searchQueryList = SerializeHelper.Deserialize<IList<SearchQuery>>(searchquery);
                }
                int catTypeID = 0;
                if (!string.IsNullOrEmpty(cattype))
                {
                    catTypeID = int.Parse(cattype);
                }
                string query = "";
                DataSet ds = searchService.SingleSearch(dbid, dbcode, searchQueryList, catTypeID, catid, "", order, pageindex, pagesize, out query, fieldlist);
                if (!string.IsNullOrEmpty(query))
                {
                    LogOpr.Search(dbid.ToString(), dbcode, query, order, "SingleSearch", "", "", "");
                }
                result.Status = Status.Success;
                result.data = ds;
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
                result.Status = Status.Error;
                result.message = ex.Message;
            }
            return result;
        }



        // POST api/search
        public void Post([FromBody]string value)
        {
        }
    }
}
